package com.iedu.demo.lgq.mapper;
import com.iedu.demo.lgq.entity.TbUser;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
@Mapper
public interface UserMapper {

    @Select("select * from tb_user where user_name=#{userName} and password = #{password}")
    TbUser login(String userName, String password);

//    @Select("select * from tb_user as sys_user where uid=#{uid}")
//    List<TbUser> selectByUid(String uid);

        //插入用户
    @Insert("insert into tb_user(user_name,password,add_date) values(#{userName},#{password},#{addDate})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void saveUser(TbUser newUser);
    //查找所有用户
    @Select("select * from tb_user as tb_user order by user_name")
    List<TbUser> findAll();

    @Select("<script>" +
            "select *" +
            "from tb_user as sys_user" +
            "<where>" +
            "<if test=\"userName != null and userName !=''\">" +
            "<bind name=\"userName\" value=\"'%'+userName+'%'\" />" +
            "and user_name like #{userName}" +
            "</if>" +
            "<if test=\"nickName != null and nickName !=''\">"+
            "<bind name=\"nickName\" value=\"'%'+nickName+'%'\" />" +
            "and nick_name like #{nickName}"+
            "</if>" +
            "</where>"+
            "</script>")
    List<TbUser>findByWhere(TbUser sysUser);

    @Update("update tb_user set state=#{state} where user_name=#{userName}")
    int updateUserStatus(@Param("userName") String userName, @Param("state") int state);

    // 更新用户头像
    @Update("update tb_user set head_picture=#{avatarUrl} where user_name=#{userName}")
    int updateUserAvatar(@Param("userName") String userName, @Param("avatarUrl") String avatarUrl);

    // 根据用户ID查找用户
    @Select("select * from tb_user where user_name=#{userName}")
    TbUser findById(String userName);

    @Update("<script>" +
            "update tb_user" +
            "<set>" +
//            "<if test=\"userName != null and userName != ''\"> user_name = #{userName}, </if>" +
            "<if test=\"realName != null and realName != ''\"> real_name = #{realName}, </if>" +
            "<if test=\"business != null and business != ''\"> business = #{business}, </if>" +
            "<if test=\"email != null and email != ''\"> email = #{email}, </if>" +
            "<if test=\"headPicture != null and headPicture != ''\"> head_picture = #{headPicture}, </if>" +
            "</set>" +
            "update_date = NOW()" +  // 设置当前时间为 updateDate
            "where id = #{id}" +
            "</script>")
    int updateUserDetails(TbUser user);

    @Select("select * from tb_user where user_name=#{userName}")
    List<TbUser> selectByUserName(String userName);

//    @Insert("insert into tb_engineer(userid,e_name,e_contact,e_address1,e_address2,skill,e_account) values (#{userid},#{eName},#{eContact},#{eAddress1},#{eAddress2},#{skill},#{eAccount})")
//    @Options(useGeneratedKeys = true,keyProperty = "eId")
//    void engineerAdd(SysEngineer newEngineer);
}